package com.swl.controller;

import com.swl.pojo.User;
import com.swl.service.ScoreService;
import com.swl.service.UserService;
import com.swl.service.impl.ScoreServiceImpl;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/userLogin")
    @ResponseBody
    public Map<String,Object> userLogin(User user, @RequestParam("img") String img, HttpServletRequest request) {
        System.out.println(user);
        Map<String,Object> hashmap=new HashMap<>();
        String session_vcode = (String) request.getSession().getAttribute("session_vcode");
        System.out.println("session_vcode=====" + session_vcode);
        System.out.println("输入的验证码：============" + img);
        User successUser = userService.selectUserByNamePwdFlag(user.getUserName(), user.getUserPwd(), user.getUserFlag());
        hashmap.put("user",successUser);
        if (session_vcode.equalsIgnoreCase(img)) {
            System.out.println("判断了验证码");
            if (successUser!=null){
                if (successUser.getUserFlag() == 1) {
                    System.out.println("111111111");
                    hashmap.put("login","1");
                    return hashmap;
                } else {
                    System.out.println("00000000");
                    hashmap.put("login","0");
                    return hashmap;
                }
            }else {
                System.out.println("用户名密码错误");
                hashmap.put("login","用户名密码错误");
                return hashmap;
            }
        }
        System.out.println("验证码错误");
        hashmap.put("login","验证码错误");
        return hashmap;
    }

    @RequestMapping("/userRegister")
    public String userRegister(User user,Model model){
         userService.insertUser(user);
//        ScoreService scoreService=new ScoreServiceImpl();
//        scoreService.insertScore(user.getUserName());
//        model.addAttribute("userName",user.getUserName());
        return "redirect:/score/insertScore?userName="+user.getUserName();
    }

    @RequestMapping("/userLoginOut")
    public String userLoginOut(HttpServletRequest request){
        System.out.println("---------管理员退出功能-------");
        HttpSession session=request.getSession();
        System.out.println(session);
        session.removeAttribute("User");
        session.invalidate();
        session=null;
        System.out.println(session);
        return "redirect:/front/login.html";
    }

   /* @RequestMapping("/gitPhoneCode")
    @ResponseBody
    public String gitPhoneCode(HttpServletRequest request){
        String userPhone=request.getParameter("userPhone");
        System.out.println("huoquyanzhenma"+userPhone);
        return userService.sendMsgToPhone(userPhone);
    }*/
}
